package com.ruoyi.procured.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.api.procuredProduct.ProcuredProductApi;
import com.ruoyi.common.api.procuredProduct.dto.ProcuredProductDto;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.procured.domain.ProcuredProduct;
import com.ruoyi.procured.service.ProcuredProductService;
import com.ruoyi.procured.mapper.ProcuredProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Admin
* @description 针对表【tb_procured_product】的数据库操作Service实现
* @createDate 2024-10-01 16:35:01
*/
@Service
public class ProcuredProductServiceImpl extends ServiceImpl<ProcuredProductMapper, ProcuredProduct>
    implements ProcuredProductService, ProcuredProductApi {

	@Autowired
	private ProcuredProductMapper procuredProductMapper;

	@Override
	public int disabledProduct(Long productId, Long planId) {
		return procuredProductMapper.disabledProduct(productId,planId);
	}

	@Override
	public List<ProcuredProductDto> selectList(List<Long> planIds, Long productId) {
		List<ProcuredProduct> procuredProducts = procuredProductMapper.selectList(
				new LambdaQueryWrapper<ProcuredProduct>()
						.in(ProcuredProduct::getPlanId,planIds)
						.eq(ProcuredProduct::getProductId,productId)
						.eq(ProcuredProduct::getDeleteStatus, 0)
		);
		return procuredProducts.stream().map(
				item -> {
					ProcuredProductDto procuredProductDto = new ProcuredProductDto();
					BeanUtils.copyProperties(item,procuredProductDto);
					return procuredProductDto;
				}
		).collect(Collectors.toList());
	}

	@Override
	public int updateProcuredProduct(Integer id, int remainingCount) {
		return procuredProductMapper.updateProcuredProduct(id,remainingCount);
	}

	@Override
	public ProcuredProductDto selectByPlanIdAndProductId(Long planId, Integer productId) {
		ProcuredProduct procuredProduct = procuredProductMapper.selectOne(
				new LambdaQueryWrapper<ProcuredProduct>()
						.eq(ProcuredProduct::getPlanId, planId)
						.eq(ProcuredProduct::getProductId, productId)
						.eq(ProcuredProduct::getDeleteStatus, Constants.NOT_DELETED_STATUS)
		);
		ProcuredProductDto dto = new ProcuredProductDto();
		BeanUtils.copyProperties(procuredProduct,dto);
		return dto;
	}

	@Override
	public int updateReturnCompletedStatus(Integer id) {
		return procuredProductMapper.updateReturnCompletedStatus(id);
	}
}




